﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.OleDb;
using System.Data;
using System.Collections;
using KitBuilder.DataAccess;

namespace KitBuilder.Views
{
    /// <summary>
    /// Interaction logic for ImportPersistence.xaml
    /// </summary>
    public partial class ImportPersistence : UserControl
    {
        private String cardFileName;
        private Dictionary<String, String> matches;
        private ArrayList stockItems = new ArrayList();
        

        public ImportPersistence(String cardFileName, Dictionary<String, String> matches)
        {
            this.cardFileName = cardFileName;
            this.matches = matches;
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void PersistOneBtn_Click(object sender, RoutedEventArgs e)
        {
            DataTable importedFile = new DataTable();

            OleDbConnection dbConnection = new OleDbConnection(
                (@"Provider=Microsoft.Jet.OLEDB.4.0;"
                 + @"Data Source=D:\Applications\tables\stock.xls;"
                 + @"Extended Properties=""Excel 8.0;HDR=Yes;"""));

            dbConnection.Open();

            try
            {
                OleDbDataAdapter dbAdapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", dbConnection);
                dbAdapter.Fill(importedFile);
            }
            finally
            {
                dbConnection.Close();
            }
            
           // foreach (DataColumn s in importedFile.Columns)
            //{
//                ListBoxItem excelItem = new ListBoxItem();
  //              excelItem.Content = s.ToString();

            //}

            foreach (DataRow r in importedFile.Rows)
            {
                DAOFactory daoFactory = new DAOFactory("NHibernate");
                StockDAO stockDAO = daoFactory.GetStockDAO();

                //ImportFileColumn_List.Items.Add(excelItem);
                //MessageBox.Show(r["aaName"].ToString());
                Dictionary<String, String> newStockItemValues = new Dictionary<string, string>();

                foreach (String s in matches.Keys)
                {
                    try
                    {
                        newStockItemValues.Add(s, r[matches[s]].ToString());
                    }
                    catch (ArgumentException ae)
                    {
                        
                    }
                }

                Stock newStockItem = new Stock(newStockItemValues);
                stockDAO.MakePersistent(newStockItem);
            }
        }
    }
}
